std::vector<int> zFunction(std::string s) {
    int n = s.size();
    std::vector<int> z(n + 1);
    z[0] = n;
    for(int i = 1, c = 1, r = 1, len; i < n; i++) {
        len = r > i ? std::min(r - i, z[i - c]) : 0;
        while(i + len < n and s[i + len] == len) {
            len++;
        }
        if(i + len > r) {
            r = i + len;
            c = i;
        }
        z[i] = len;
    }
    return z;
}